package com.treasure.hunt.popup

import android.app.Activity
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.view.View
import android.view.animation.Animation
import android.widget.ImageView
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTNativeExpressAd
import com.module.lemlin.base.loadCenterImage
import com.module.lemlin.base.loadCropImage
import com.treasure.hunt.R
import com.treasure.hunt.app.AppConstance
import com.treasure.hunt.config.TTAdManagerHolder
import com.treasure.hunt.entity.*
import kotlinx.android.synthetic.main.popup_dialog_flop.view.*
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.ScaleConfig

class DialogFlopPopup(
    context: Context,
    val onAnimationEnd: (imageView: ImageView) -> Unit,
    val onDoubled: (luckySign: LuckySign) -> Unit = {}
) : BasePopupWindow(context) {

    init {
        setBlurBackgroundEnable(true)
    }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_dialog_flop)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.tvDialogGiftReceive.setOnClickListener {
            dismiss()
            val imageView: ImageView = contentView.ivDialogGiftIcon.apply {
                tag = mLuckySign != null
            }
            onAnimationEnd(imageView)
        }
        contentView.tvDialogGiftDoubled.setOnClickListener {
            dismiss()
            mLuckySign?.let {
                onDoubled(it)
            }
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withScale(ScaleConfig.CENTER)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {

        return AnimationHelper.asAnimation()
            .withScale(ScaleConfig.CENTER)
            .toDismiss().apply {
                setAnimationListener(object : Animation.AnimationListener {
                    override fun onAnimationRepeat(animation: Animation?) {

                    }

                    override fun onAnimationEnd(animation: Animation?) {
                        //onAnimationEnd(contentView.ivDialogGiftIcon)
                    }

                    override fun onAnimationStart(animation: Animation?) {

                    }
                })
            }
    }

    fun fillData(luckyGift: LuckyGift): DialogFlopPopup {
        mLuckySign = null
        contentView.flDialogGiftAd.removeAllViews()
        if (!luckyGift.image.isNullOrEmpty()) {
            val isHttps: Boolean = luckyGift.image.substring(0, 5).contains("http") ||
                    luckyGift.image.substring(0, 5).contains("https")
            contentView.ivDialogGiftIcon.loadCenterImage("${if (isHttps) "" else AppConstance.URL_IMAGE}${luckyGift.image}")
        }
        contentView.tvDialogGiftTitle.text = luckyGift.title
        contentView.tvDialogGiftDoubled.visibility = View.GONE
        return this
    }

    fun fillData(luckyFlop: LuckyFlop): DialogFlopPopup {
        mLuckySign = null
        contentView.flDialogGiftAd.removeAllViews()
        luckyFlop.image?.let { image ->
            val isHttps: Boolean = image.substring(0, 5).contains("http") ||
                    image.substring(0, 5).contains("https")
            contentView.ivDialogGiftIcon.loadCenterImage("${if (isHttps) "" else AppConstance.URL_IMAGE}${image}")
        }
        contentView.tvDialogGiftTitle.text = luckyFlop.title
        contentView.tvDialogGiftDoubled.visibility = View.GONE
        return this
    }

    fun fillData(luckyGrid: LuckyGrid): DialogFlopPopup {
        mLuckySign = null
        contentView.flDialogGiftAd.removeAllViews()
        if (!luckyGrid.image.isNullOrEmpty()) {
            val isHttps: Boolean = luckyGrid.image.substring(0, 5).contains("http") ||
                    luckyGrid.image.substring(0, 5).contains("https")
            contentView.ivDialogGiftIcon.loadCenterImage("${if (isHttps) "" else AppConstance.URL_IMAGE}${luckyGrid.image}")
        }
        contentView.tvDialogGiftTitle.text = luckyGrid.title
        contentView.tvDialogGiftDoubled.visibility = View.GONE
        return this
    }

    fun fillData(videoEnd: VideoEnd): DialogFlopPopup {
        mLuckySign = null
        contentView.flDialogGiftAd.removeAllViews()
        if (!videoEnd.image.isNullOrEmpty()) {
            val isHttps: Boolean = videoEnd.image.substring(0, 5).contains("http") ||
                    videoEnd.image.substring(0, 5).contains("https")
            contentView.ivDialogGiftIcon.loadCenterImage("${if (isHttps) "" else AppConstance.URL_IMAGE}${videoEnd.image}")
        }
        contentView.tvDialogGiftTitle.text = videoEnd.text
        contentView.tvDialogGiftDoubled.visibility = View.GONE
        return this
    }

    fun fillData(luckyReceive: LuckyReceive): DialogFlopPopup {
        mLuckySign = null
        contentView.flDialogGiftAd.removeAllViews()
        if (!luckyReceive.image.isNullOrEmpty()) {
            val isHttps: Boolean = luckyReceive.image.substring(0, 5).contains("http") ||
                    luckyReceive.image.substring(0, 5).contains("https")
            contentView.ivDialogGiftIcon.loadCenterImage("${if (isHttps) "" else AppConstance.URL_IMAGE}${luckyReceive.image}")
        }
        contentView.tvDialogGiftTitle.text = luckyReceive.text
        contentView.tvDialogGiftDoubled.visibility = View.GONE
        return this
    }

    private var mLuckySign: LuckySign? = null

    fun fillData(luckySign: LuckySign): DialogFlopPopup {
        this.mLuckySign = luckySign
        contentView.flDialogGiftAd.removeAllViews()
        if (!luckySign.image.isNullOrEmpty()) {
            val isHttps: Boolean = luckySign.image.substring(0, 5).contains("http") ||
                    luckySign.image.substring(0, 5).contains("https")
            contentView.ivDialogGiftIcon.loadCenterImage("${if (isHttps) "" else AppConstance.URL_IMAGE}${luckySign.image}")
        }
        contentView.tvDialogGiftTitle.text = luckySign.text
        contentView.tvDialogGiftDoubled.visibility =
            if (luckySign.double_switch == 1) View.VISIBLE else View.GONE
        return this
    }

    fun fillViewAd(view: View) {
        contentView.flDialogGiftAd.removeAllViews()
        contentView.flDialogGiftAd.addView(view)
    }
}